﻿
@{
    ViewBag.Title = "权限管理";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}
<table id="tgMenu"></table> 
<div id="tbar">
    <div class="tbar">
        <div class="z-toolbar">
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-redo',plain:true" onclick="ToolbarRedo();">展开</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="ToolbarUndo();">折叠</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-arrow_refresh',plain:true" onclick="ToolbarRefresh();">刷新</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="ToolbarAdd();">增加</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="ToolbarEdit();">编辑</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cross',plain:true" onclick="ToolbarRemove();">删除</a>
            <div class="datagrid-btn-separator"></div>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="ToolbarUnselect();">取消选中</a>
        </div>
    </div>
</div>
@section PageSpecificScript{
    <script type="text/javascript">
        var tgMenu;/*显示权限*/
        $(document).ready(function () {
            tgMenu = $("#tgMenu").treegrid({
                iconCls: 'icon-save',
                fit: true,
                fitColumns: true,
                rownumbers: true,
                nowrap: false,
                animate: false,
                idField: 'PId',
                treeField: 'PName',
                toolbar: '#tbar',
                url: '/Admin/UO_Permission/GetAllPersForTreeGridPer?id=' + xz.UUID(),
                frozenColumns: [[
                    { field: "PId", title: "权限编号", hidden: true },
                    {
                        field: "PName", title: "权限名称", align: 'center', width: 200,
                        formatter: function (value, row, index) {
                            if (value) {
                                return xz.fs('<span title="{0}">{1}</span>', value, value);
                            }
                        }
                    }
                ]],
                columns: [[
                    {title: " 权限地址",colspan: 4},
                    {
                        title: "权限图标",
                        field: "PIcon",
                        width: 40,
                        rowspan: 2,
                        align: 'center',
                        formatter: function (value, row, index) {
                            if (!value) {
                                return "";
                            } else {
                                return xz.fs('<span class="{0}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>', value);
                            }
                        }

                    }, {
                        title: "是否可见",
                        field: "PIsShow",
                        align: 'center',
                        rowspan: 2,
                        width: 40,
                        formatter: xz.formatterEnable
                    }, {
                        title: "排序",
                        field: "POrder",
                        rowspan: 2,
                        align: 'center',
                        width: 40,
                    },
                    {
                        title: "权限说明",
                        field: "PRemark",
                        align: 'center',
                        hidden: true,
                        rowspan: 2,
                        width: 100
                    },
                    {
                        title: "操作日期",
                        field: "PUpdateTime",
                        align: 'center',
                        rowspan: 2,
                        hidden: true,
                        width: 100,
                        formatter: function (value, row, index) {
                            if (value) {
                                return eval("new " + value.substr(1, value.length - 2)).toLocaleDateString();
                            }
                        }
                    }


                ], [{
                    title: "区域",
                    field: "PAreaName",
                    align: 'center',
                    width: 60
                }, {
                    title: "控制器",
                    field: "PControllerName",
                    align: 'center',
                    width: 80
                }, {
                    title: "操作方式",
                    field: "PActionName",
                    align: 'center',
                    width: 80
                }, {
                    title: "请求方法",
                    field: "PFormMethod",
                    align: 'center',
                    width: 40,
                    formatter: function (value) {
                        return value == 1 ? 'GET' : value == 2 ? 'POST' : 'HEAD';
                    }
                }]],

                onContextMenu: function (e, row) {
                    e.preventDefault();
                    $(this).treegrid('unselectAll');
                    $(this).treegrid('select', row.pid);
                    $('#menu').menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    });
                },
                onExpand: function (row) {
                    tgMenu.treegrid('unselectAll');
                }
                ,
                onCollapse: function (row) {
                    tgMenu.treegrid('unselectAll');
                }
            });
        })
        /**展开菜单**/
        function ToolbarRedo() {
            var node = tgMenu.treegrid("getSelected");
            if (node) {
                tgMenu.treegrid("expand", node.Pid);
            } else {
                tgMenu.treegrid("expandAll");
            }
        }
        /**折叠菜单**/
        function ToolbarUndo() {
            var node = tgMenu.treegrid("getSelected");
            if (node) {
                tgMenu.treegrid("collapse", node.Pid);
            } else {
                tgMenu.treegrid("collapseAll");
            }
        }
        /**刷新菜单**/
        function ToolbarRefresh() {
            tgMenu.treegrid("reload");
        }
        /**增加菜单**/
        function ToolbarAdd() {
            showAppendView();
        }
        /**编辑菜单**/
        function ToolbarEdit() {
            showEditView();
        }
        /**删除菜单**/
        function ToolbarRemove() {
            removeDept();
        }
        /**取消选中菜单**/
        function ToolbarUnselect() {
            tgMenu.treegrid("unselectAll");
        }
        /*打开添加权限信息的页面*/
        function showAppendView() {
            $.validateUrl('/Admin/UO_Permission/EditView', append, xz.dealWith, 'Get');
        }

        /*添加权限信息*/
        function append() {
            var pAdd = xz.dialog({
                title: '新增权限',
                width: 670,
                height: 400,
                iconCls: 'icon-add',
                href: '/Admin/UO_Permission/EditView',
                buttons: [{
                    text: '新增',
                    iconCls: 'icon-add',
                    handler: function () {
                        var f = pAdd.find('form');
                        f.form('submit', {
                            url: '/Admin/UO_Permission/AddPermission',
                            success: function (jsonData) {
                                jsonData = $.parseJSON(jsonData);
                                $.procAjaxMsg(jsonData, function () {
                                    pAdd.dialog('close');
                                    tgMenu.treegrid("reload");
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                }, function () {
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                });
                            }
                        });
                    }
                }, {
                    text: '关闭',
                    iconCls: 'icon-cancel',
                    handler: function () {
                        pAdd.dialog('close');
                    }
                }],
                onLoad: function () {
                    var f = pAdd.find('form');
                    var pName = f.find('input[name=PName]');
                    var pParentID = f.find('input[name=PParentID]');
                    var pIcon = f.find('input[name=PIcon]');

                    window.setTimeout(function () {
                        pName.focus();
                    }, 100);

                    pParentID.combotree({
                        url: '/Admin/UO_Permission/GetAllPermissions',
                        required: true,
                        lines: true,
                    });

                    pIcon.combobox({
                        url: '/Admin/Sys_Icons/GetAllIcons',
                        required: true,
                        formatter: function (row) {
                            return xz.fs('{0}-<span class="{1}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>', row.text, row.value);
                        }
                    });


                }
            })
        }

        /*增加权限信息*/
        function showEditView() {
            $.validateUrl('/Admin/UO_Permission/EditView', edit, xz.dealWith, 'Get');
        }
        /*修改权限信息*/
        function edit() {
            var node = tgMenu.treegrid("getSelected");
            if (node) {
                var pAdd = xz.dialog({
                    title: '修改权限',
                    width: 670,
                    height: 400,
                    iconCls: 'icon-edit',
                    href: '/Admin/UO_Permission/EditView',
                    buttons: [{
                        text: '修改',
                        iconCls: 'icon-edit',
                        handler: function () {
                            var f = pAdd.find('form');
                            f.form('submit', {
                                url: '/Admin/UO_Permission/EditPermission',
                                success: function (jsonData) {
                                    jsonData = $.parseJSON(jsonData);
                                    $.procAjaxMsg(jsonData, function () {
                                        pAdd.dialog('close');
                                        tgMenu.treegrid("reload", node._parentId);
                                        $.alertMsg(jsonData.Msg, "操作提示", null);
                                    }, function () {
                                        $.alertMsg(jsonData.Msg, "操作提示", null);
                                    });
                                }
                            });
                        }
                    }, {
                        text: '关闭',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            pAdd.dialog('close');
                        }
                    }],
                    onLoad: function () {
                        var f = pAdd.find('form');
                        var pName = f.find('input[name=PName]');
                        var pParentID = f.find('input[name=PParentID]');
                        var pIcon = f.find('input[name=PIcon]');

                        window.setTimeout(function () {
                            pName.focus();
                        }, 100);

                        pParentID.combotree({
                            url: '/Admin/UO_Permission/GetAllPermissions',
                            required: true,
                            lines: true,
                        });

                        pIcon.combobox({
                            url: '/Admin/Sys_Icons/GetAllIcons',
                            required: true,
                            formatter: function (row) {
                                return xz.fs('{0}-<span class="{1}" style="display:inline-block;vertical-align:middle;width:16px;height:16px;"></span>', row.text, row.value);
                            }
                        });

                        /*把选中的组织机构信息绑定到对应的表单上*/
                        f.form('load', node);
                    }
                })
            } else {
                xz.messageAlert("提示", "请选择要修改的组织结构", "error");
            }
        }

        /*删除权限*/
        function removeDept() {
            var node = tgMenu.treegrid("getSelected");
            if (node) {
                xz.messageConfirm('询问', '你确定要删除【' + node.PName + '】吗?', function (b) {
                    if (b) {
                        $.ajax({
                            type: 'POST',
                            url: '/Admin/UO_Permission/DelPermission',
                            data: {
                                pid: node.Pid
                            },
                            cache: false,
                            dataType: 'JSON',
                            success: function (jsonData) {
                                $.procAjaxMsg(jsonData, function () {
                                    tgMenu.treegrid("reload", node._parentId);
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                }, function () {
                                    $.alertMsg(jsonData.Msg, "操作提示", null);
                                });
                            }
                        });
                    }
                });
            }
        }
    </script>
}